﻿// This file is part of Code4Public (http://code4public.codeplex.com)
// Copyright 2011 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
// ------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Code4Public.Settings;

namespace Code4Public.GUI
{
    public partial class PropertiesForm : Form
    {
        public PropertiesForm()
        {
            InitializeComponent();

            LoadDefaultValues();
        }

        /// <summary>
        /// Loads the default values for the components.
        /// </summary>
        private void LoadDefaultValues()
        {
            if (RunTimeParams.MaxHeight <= 0)
            {
                cbHeightAll.Checked = true;
            }
            else
            {
                cbHeightAll.Checked = false;
                numericUpDownHeight.Value = RunTimeParams.MaxHeight;
            }

            if (RunTimeParams.MaxWidth <= 0)
            {
                cbWidthAll.Checked = true;
            }
            else
            {
                cbWidthAll.Checked = false;
                numericUpDownWidth.Value = RunTimeParams.MaxWidth;
            }
        }

        /// <summary>
        /// Shows the properties dialog.
        /// </summary>
        /// <returns>true if the user pressed OK and a change is needed; false otherwise.</returns>
        public static bool ShowPropertiesDialog()
        {
            using (PropertiesForm frm = new PropertiesForm())
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (frm.cbWidthAll.Checked)
                    {
                        RunTimeParams.MaxWidth = 0;
                    }
                    else
                    {
                        RunTimeParams.MaxWidth = Convert.ToInt32(frm.numericUpDownWidth.Value);
                    }

                    if (frm.cbHeightAll.Checked)
                    {
                        RunTimeParams.MaxHeight = 0;
                    }
                    else
                    {
                        RunTimeParams.MaxHeight = Convert.ToInt32(frm.numericUpDownHeight.Value);
                    }

                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the cbWidthAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbWidthAll_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownWidth.Enabled = !cbWidthAll.Checked;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the cbHeightAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbHeightAll_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownHeight.Enabled = !cbHeightAll.Checked;
        }
    }
}
